package com.guoyansoft.model.service.impl;

import com.guoyansoft.config.PasswordEncryption;
import com.guoyansoft.config.R;
import com.guoyansoft.model.dao.ManagerDao;
import com.guoyansoft.model.dao.ShopManagerDao;
import com.guoyansoft.model.pojo.Manager;
import com.guoyansoft.model.pojo.Shop;
import com.guoyansoft.model.pojo.ShopManager;
import com.guoyansoft.model.service.MangerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class MangerServiceImpl implements MangerService {
    @Autowired
    private ShopManagerDao shopManagerDao;
    @Autowired
    private ManagerDao managerDao;



    @Override
    public R save(ShopManager shopManager) {
        Manager manager = shopManager.getManager();
        manager.setPass(PasswordEncryption.getCiphertext(manager.getPass()));
        int row1 = managerDao.insert(manager);
        int row2 = shopManagerDao.insert(shopManager);
        return row1>0&&row2>0?R.suc():R.fail();
    }

    @Override
    public R modify(ShopManager shopManager) {
        Manager manager = shopManager.getManager();
        manager.setPass(PasswordEncryption.getCiphertext(manager.getPass()));
        int row1 = managerDao.update(shopManager.getManager());
        int row2 = shopManagerDao.update(shopManager);
        return row1>0&&row2>0?R.suc():R.fail();
    }

    @Override
    public R drop(Integer[] id) {
        for (Integer ids : id) {
            int row1 = managerDao.delete(ids);
            int row2 = shopManagerDao.delete(ids);
            if(row1<=0||row2<=0){
                return R.fail();
            }
        }
        return R.suc();
    }

    @Override
    public R queryByPage(Map<String, Object> params) {
        return R.suc(managerDao.selectTotal(params),managerDao.selectByPage(params));
    }

    @Override
    public R queryOfLogin(Manager manager) {
        manager.setPass(PasswordEncryption.getCiphertext(manager.getPass()));
        return R.suc(managerDao.selectOfLogin(manager));
    }

    @Override
    public R queryShop(Integer managerId) {
        Shop shop = shopManagerDao.selectShop(managerId);
        return R.suc(shop);
    }

    @Override
    public R selectByshopId(Map<String,Object> params) {
        List<Manager> managerList =managerDao.selectByshopId(params);
        return R.suc(managerList);
    }

}
